package com.zhuiyun.project.api.caseRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyun.project.api.caseRecord.entity.CaseRecord;
import com.zhuiyun.project.api.caseRecord.mapper.CaseRecordMapper;
import com.zhuiyun.project.api.caseRecord.service.CaseRecordService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.ObjectUtils;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CaseServiceImpl
 * @Description TODO 案例管理业务层实现类
 * @Author lpr
 * @Date 2023/11/2 20:22
 **/
@Service
public class CaseRecordServiceImpl implements CaseRecordService {
    @Resource
    CaseRecordMapper caseRecordMapper;
    /**
    * 分页查询+模糊查询 案例管理
    * @Author lpr
    * @Description
    * @Date 2023/11/2 20:53
    * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.zhuiyun.project.api.caseRecord.entity.CaseRecord>
    * @param pageRequest
    * @param caseRecord
    */
    @Override
    public IPage<CaseRecord> selectCaseRecord(PageRequest pageRequest, CaseRecord caseRecord) {
        List<CaseRecord> caseRecords = null;
        caseRecords = caseRecordMapper.selectCaseRecord(caseRecord);
        // 设置分页规则
        Page<CaseRecord> dictDataEntityPage = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        // 当前页码
        int pageNum = pageRequest.getPageNum();
        // 每页记录数
        int pageSize = pageRequest.getPageSize();
        // 计算起始索引
        int startIndex = (pageNum - 1) * pageSize;
        // 计算结束索引
        int endIndex = Math.min(startIndex + pageSize, caseRecords.size());
        // 截取当前页数据
        List<CaseRecord> currentPageData = caseRecords.subList(startIndex, endIndex);
        // 将当前页数据存储到分页对象的记录列表中
        dictDataEntityPage.setRecords(currentPageData);
        // 设置总条数，仍然使用原始的总记录数
        dictDataEntityPage.setTotal(caseRecords.size());
        return dictDataEntityPage;
    }
    /**
     * 修改案例状态
     * @Author lpr
     * @Description
     * @Date 2023/11/03 13:52
     * @Return java.lang.Object
     * @param caseRecord
     */
    @Override
    public Object editCaseStatus(CaseRecord caseRecord) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("case_id", caseRecord.getCaseId());
        return caseRecordMapper.update(caseRecord,wrapper);
    }
    /**
    * 案例逻辑删除
    * @Author lpr
    * @Description
    * @Date 2023/11/9 15:56
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param caseId
    */
    @Override
    public CommonResult removeCaseById(Integer caseId) {
        CaseRecord caseRecord = new CaseRecord();
        caseRecord.setDelFlag(1);
        UpdateWrapper<CaseRecord> updateWrapper = Wrappers.update();
        updateWrapper.eq("case_id",caseId);
        caseRecordMapper.update(caseRecord,updateWrapper);
        return CommonResult.ok(EmErrorCode.DELETE_OK);
    }
}
